package com.hospital.register.user.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hospital.register.common.result.Result;
import com.hospital.register.common.result.ResultCodeEnum;
import com.hospital.register.dto.user.PatientQueryDto;
import com.hospital.register.dto.user.UserQueryDto;
import com.hospital.register.enums.AuthStatusEnum;
import com.hospital.register.user.service.PatientService;
import com.hospital.register.user.service.UserInfoService;
import com.hospital.register.vo.user.PatientQueryVo;
import com.hospital.register.vo.user.UserAndPatients;
import com.hospital.register.vo.user.UserQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Min;
import java.util.List;

@Api(tags = "用户管理")
@RestController
@RequestMapping("/admin/user")
@Validated
public class UserInfoAdminController {

    /**
     * 处理用户相关业务
     */
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 处理就诊人相关业务
     */
    @Autowired
    private PatientService patientService;

    @ApiOperation("分页查询用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码（从1开始）", paramType = "path", dataTypeClass = Integer.class, required = true),
            @ApiImplicitParam(name = "limit", value = "每页条数", paramType = "path", dataTypeClass = Integer.class, required = true)
    })
    @GetMapping("/{page}/{limit}")
    public Result<Page<UserQueryVo>> selectList(
            @PathVariable @Min(value = 1, message = "页码不可小于 1") Integer page,
            @PathVariable @Range(min = 1L, max = 50, message = "每页长度必须在1到50之间") Integer limit,
            @Validated UserQueryDto findingConditions) {
        Page<UserQueryVo> pageConditions = new Page<>(page, limit);
        return Result.ok(this.userInfoService.selectPage(pageConditions, findingConditions));
    }

    @ApiOperation("锁定或解锁用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户 id", paramType = "path", dataTypeClass = Long.class, required = true),
            @ApiImplicitParam(name = "status", value = "状态值", paramType = "path", dataTypeClass = Byte.class, required = true)
    })
    @GetMapping("/lock/{userId}/{status}")
    public Result lock(
            @Min(value = 1L, message = "id 非法") @PathVariable Long userId,
            @Range(min = 0L, max = 1L, message = "状态值非法") @PathVariable Byte status) {
        this.userInfoService.updateStatus(userId, status);
        return Result.ok();
    }

    @ApiOperation("查询用户极其关联的所有就诊人")
    @ApiImplicitParam(name = "userId", value = "用户 id", paramType = "path", dataTypeClass = Long.class, required = true)
    @GetMapping("/show/{userId}")
    public Result<UserAndPatients> show(
            @Min(value = 1L, message = "id 非法") @PathVariable Long userId) {
        //查询用户
        UserQueryVo user = this.userInfoService.selectById(userId);

        //查询齐下的就诊人
        if (user == null) {
            return Result.ok();
        }

        PatientQueryDto patientCondition = new PatientQueryDto();
        patientCondition.setUserId(userId);
        List<PatientQueryVo> patients = this.patientService.selectList(patientCondition);

        //封装用户和就诊人列表
        UserAndPatients userAndPatients = new UserAndPatients();
        userAndPatients.setUserInfo(user);
        userAndPatients.setPatientList(patients);
        return Result.ok(userAndPatients);
    }

    @ApiOperation("审批认证信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户 id", paramType = "path", dataTypeClass = Long.class, required = true),
            @ApiImplicitParam(name = "authStatus", value = "审批结果", paramType = "path", dataTypeClass = Byte.class, required = true)
    })
    @GetMapping("/approval/{userId}/{authStatus}")
    public Result approval(
            @PathVariable @Min(value = 1L, message = "id 非法") Long userId,
            @PathVariable @Range(min = -1L, max = 2L, message = "审批结果非法") Byte authStatus) {
        if (AuthStatusEnum.AUTH_SUCCESS.getStatus().equals(authStatus)//认证通过
                || AuthStatusEnum.AUTH_FAIL.getStatus().equals(authStatus)) {//或认证失败
            this.userInfoService.updateAuthStatus(userId, AuthStatusEnum.AUTH_RUN.getStatus(), authStatus);
            return Result.ok();
        } else {
            return Result.build(ResultCodeEnum.PARAM_ERROR.getCode(), "审批结果非法");
        }
    }

}
